package com.foodorder.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 用户实体类 - 增强版本
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "users", indexes = {
    @Index(name = "idx_user_email", columnList = "email"),
    @Index(name = "idx_user_wx_open_id", columnList = "wxOpenId"),
    @Index(name = "idx_user_role", columnList = "role"),
    @Index(name = "idx_user_status", columnList = "status")
})
@EntityListeners(AuditingEntityListener.class)
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 邮箱（登录账号）
     */
    @Column(unique = true, nullable = false, length = 100)
    private String email;
    
    /**
     * 密码
     */
    @Column(nullable = false)
    private String password;
    
    /**
     * 昵称
     */
    @Column(length = 50, nullable = false)
    private String nickname;
    
    /**
     * 手机号
     */
    @Column(length = 20)
    private String phone;
    
    /**
     * 头像URL
     */
    @Column(name = "avatar_url", length = 255)
    private String avatar;
    
    /**
     * 性别
     */
    @Enumerated(EnumType.STRING)
    @Column(length = 10)
    private Gender gender;
    
    /**
     * 生日
     */
    @Column
    private LocalDateTime birthday;
    
    /**
     * 微信OpenID
     */
    @Column(name = "wx_open_id", length = 100)
    private String wxOpenId;
    
    /**
     * 用户位置经度
     */
    @Column(precision = 11)
    private Double longitude;
    
    /**
     * 用户位置纬度
     */
    @Column(precision = 10)
    private Double latitude;
    
    /**
     * 用户角色：顾客、商家、管理员
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private UserRole role = UserRole.CUSTOMER;
    
    /**
     * 关联店铺ID（仅商家用户有值）
     */
    @Column(name = "store_id")
    private Long storeId;
    
    /**
     * 权限级别
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "permission_level", nullable = false, length = 20)
    private PermissionLevel permissionLevel = PermissionLevel.BASIC;
    
    /**
     * 用户状态：ACTIVE-正常，DISABLED-禁用
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private UserStatus status = UserStatus.ACTIVE;
    
    /**
     * 邮箱是否已验证
     */
    @Column(name = "email_verified", nullable = false)
    private Boolean emailVerified = false;
    
    /**
     * Token版本号（用于全局token失效）
     */
    @Column(name = "token_version", nullable = false)
    private Long tokenVersion = 1L;
    
    /**
     * 最后登录时间
     */
    @Column(name = "last_login_at")
    private LocalDateTime lastLoginAt;
    
    /**
     * 最后登录IP
     */
    @Column(name = "last_login_ip", length = 45)
    private String lastLoginIp;
    
    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    /**
     * 用户角色枚举
     */
    public enum UserRole {
        CUSTOMER,    // 顾客 - 小程序用户
        MERCHANT,    // 商家 - 商家后台用户
        ADMIN        // 管理员 - 管理员后台用户
    }
    
    /**
     * 权限级别枚举
     */
    public enum PermissionLevel {
        BASIC,       // 基础权限
        ADVANCED,    // 高级权限
        SUPER        // 超级权限
    }
    
    /**
     * 用户状态枚举
     */
    public enum UserStatus {
        ACTIVE,      // 正常
        INACTIVE,    // 未激活
        DISABLED,    // 禁用
        BANNED       // 封禁
    }
    
    /**
     * 性别枚举
     */
    public enum Gender {
        MALE,        // 男
        FEMALE,      // 女
        UNKNOWN      // 未知
    }
    
    /**
     * 增加Token版本号（用于强制下线）
     */
    public void incrementTokenVersion() {
        this.tokenVersion++;
    }
    
    /**
     * 更新最后登录信息
     */
    public void updateLastLogin(String ip) {
        this.lastLoginAt = LocalDateTime.now();
        this.lastLoginIp = ip;
    }
    
    /**
     * 检查用户是否激活
     */
    public boolean isActive() {
        return UserStatus.ACTIVE.equals(this.status);
    }
    
    /**
     * 检查用户是否为管理员
     */
    public boolean isAdmin() {
        return UserRole.ADMIN.equals(this.role);
    }
    
    /**
     * 检查用户是否为商家
     */
    public boolean isMerchant() {
        return UserRole.MERCHANT.equals(this.role);
    }
} 